//
//  PreferentialRightViewController.m
//  Project_A Gary
//
//  Created by leilei on 16/4/1.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "PreferentialRightViewController.h"
#import "PreferentialRightDetailViewController.h"
#import "PreferentialRightModel.h"
#import "PreferentialRightTableViewCell.h"




@interface PreferentialRightViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *dataArr;

@end

@implementation PreferentialRightViewController
-(void)dealloc
{
    [_tableV release];
    [_dataArr release];
    [super dealloc];
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
    
    [self createTableView];
    [self dataHandle];
}



#pragma mark - 必须写的两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PreferentialRightTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PreferentialRightTableViewCell" forIndexPath:indexPath];
    PreferentialRightModel *rightModel = self.dataArr[indexPath.row];
    
    cell.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1];
    cell.title.text = rightModel.title;
    cell.maxDiscount.text = [NSString stringWithFormat:@"%@", rightModel.maxDiscount];
    cell.serialGroupNames.text = rightModel.serialGroupNames;
    cell.endTime.text = rightModel.endTime;
    
    NSString *str = @"已报名#人";
    cell.orderCount.text = [str stringByReplacingOccurrencesOfString:@"#" withString:[NSString stringWithFormat:@"%@", rightModel.orderCount]];
    
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:rightModel.photo]];
    
    return cell;
}


#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.rowHeight = 150;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableV];
    [_tableV release];
    [self.tableV registerClass:[PreferentialRightTableViewCell class] forCellReuseIdentifier:@"PreferentialRightTableViewCell"];
    
    // tableView 的边界
    self.tableV.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
}

#pragma mark - 解析数据
- (void)dataHandle
{
    NSString *str = @"http://mrobot.pcauto.com.cn/v3/price/activityListv45/248?pageSize=20&areaId=248&pageNo=1";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
        self.dataArr = [PreferentialRightModel transformWithArray:responseObject[@"data"]];
        [_tableV reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}



#pragma mark - 单例的方法实现
+ (PreferentialRightViewController *)singleInstance
{
    static PreferentialRightViewController *rightVC = nil;
    if (!rightVC) {
        rightVC = [[PreferentialRightViewController alloc] init];
    }
    return rightVC;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
